package com.lufei4.chestnut.orbyun.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lufei4.chestnut.R;
import com.lufei4.chestnut.network.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: Chestnut
 * @Package: com.lufei4.chestnut.orbyun.db
 * @ClassName: DBActivity
 * @Description: java类作用描述
 * @Author: liushijiang 15833013864@163.com
 * @CreateDate: 2020/9/25 14:02
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/9/25 14:02
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class DBActivity extends BaseActivity {
    @BindView(R.id.button3)
    Button button3;
    @BindView(R.id.button4)
    Button button4;
    @BindView(R.id.button5)
    Button button5;
    @BindView(R.id.button6)
    Button button6;
    @BindView(R.id.button7)
    Button button7;
    @BindView(R.id.button8)
    Button button8;
    @BindView(R.id.button9)
    Button button9;
    @BindView(R.id.button10)
    Button button10;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_address)
    EditText editAddress;
    @BindView(R.id.tv)
    TextView tv;

    @Override
    public int getLayoutId() {
        return R.layout.activity_db;
    }

    @Override
    public void initView() {

    }

    private String name, address;

    @OnClick({R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8, R.id.button9, R.id.button10})
    public void onViewClicked(View view) {
        if (editName.getText().toString().trim().isEmpty() && editAddress.getText().toString().trim().isEmpty()) {
            ToastUtils.showShort("两个参数均为空");
            return;
        }

        name = editName.getText().toString().trim();
        address = editAddress.getText().toString().trim();
        switch (view.getId()) {
            case R.id.button3:
                creDB();
                break;
            case R.id.button4:
                WritableDataBase(name, address);
                break;
            case R.id.button5:
                readDB(name, address);
                break;
            case R.id.button6:
                readDBAll();
                break;
            case R.id.button7:
                break;
            case R.id.button8:
                break;
            case R.id.button9:
                break;
            case R.id.button10:
                break;
        }
    }

    private MySQLiteHelper blcs;
    private SQLiteDatabase writableDatabase;

    private void creDB() {
        blcs = MySQLiteHelper.getInstance(this);
    }

    private void WritableDataBase(String name, String address) {
        writableDatabase = blcs.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("address", address);
        writableDatabase.insert("SqliteDemo", null, contentValues);
    }

    private void del(String name, String address) {
        String delete = new StringBuilder().append("delete from SqliteDemo where name = '").append(name).append("' or address = '").append(address).append("'").toString();
        writableDatabase.execSQL(delete);
    }

    private void readDB(String name, String address) {
        StringBuilder append = new StringBuilder().append("select * from SqliteDemo where name = '")
                .append(name).append("'").append("  and address = '")
                .append(address).append("'");
        Cursor cursor = writableDatabase.rawQuery(append.toString(), null);
        ArrayList<SqliteDemo> data = getData(cursor);
        tv.setText(data.toString());
    }

    private void readDBAll() {
        blcs=MySQLiteHelper.getInstance(this);
        writableDatabase=blcs.getWritableDatabase();
        StringBuilder append = new StringBuilder().append("select * from SqliteDemo");

        Cursor cursor = writableDatabase.rawQuery(append.toString(), null);
        ArrayList<SqliteDemo> data = getData(cursor);
        tv.setText(data.toString());
    }

    private ArrayList<SqliteDemo> getData(Cursor cursor) {
        ArrayList<SqliteDemo> dats = new ArrayList<>();
        while (cursor.moveToNext()) {
            SqliteDemo sqliteDemo = new SqliteDemo();
            long id = cursor.getLong(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String address = cursor.getString(cursor.getColumnIndex("address"));
            sqliteDemo.id = id;
            sqliteDemo.address = address;
            sqliteDemo.name = name;
            dats.add(sqliteDemo);
        }
        cursor.close();
        return dats;
    }


}
